package com.starssea.mybatis.mapper;

import com.starssea.mybatis.po.ArticleEntity;
import com.starssea.mybatis.po.CommentEntity;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ArticleOneToManyMapper {

    @Select("""
            select id, article_id, content from comment where article_id = #{articleId}
            order by id
            """)
    @Results(id = "commentMapper", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "article_id", property = "articleId"),
            @Result(column = "content", property = "content")
    })
    List<CommentEntity> selectCommentsByArticleId(Integer articleId);

    @Select("""
            select id, user_id, title, summary, read_count, create_time, update_time
            from article where id = #{id}
            """)
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "title", property = "title"),
            @Result(column = "summary", property = "summary"),
            @Result(column = "read_count", property = "readCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "id", property = "comments",
                    many = @Many(select = "com.starssea.mybatis.mapper.ArticleOneToManyMapper.selectCommentsByArticleId",
                            fetchType = FetchType.LAZY))
    })
    ArticleEntity selectArticleById(Integer id);
}
